package edu.unindra.company.dao.impl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;
import java.util.List;

import edu.unindra.company.dao.entity.Empl;
import edu.unindra.company.dao.intrf.EmployeeInterf;
import edu.unindra.company.dao.koneksi.DatabaseUtilities;

public class EmployeeImpl implements EmployeeInterf {

	@Override
	public int insert(Empl empl) throws SQLException {
		int result = -1;
		PreparedStatement ps = null;
		try {
			ps = DatabaseUtilities
					.getInstance()
					.getConnection()
					.prepareStatement(
							"insert into employee values (?,?,?,?,?,?,?,?,?,?)");

			ps.setInt(1, empl.getNim());
			ps.setString(2, empl.getNama());
			ps.setString(3, empl.getTeTgl());
			ps.setString(4, empl.getTaTgl());
			ps.setString(5, empl.getjKelamin());
			ps.setString(6, empl.getJabatan());
			ps.setInt(7, empl.getmKerja());
			ps.setInt(8, empl.getKontak());
			ps.setString(9, empl.getPhoto());
			ps.setString(10, empl.getAlamat());

			result = ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			ps.close();
		}

		return result;
	}

	@Override
	public int update(Empl empl) throws SQLException {
		int result = -1;

		PreparedStatement ps = null;
		try {
			ps = DatabaseUtilities
					.getInstance()
					.getConnection()
					.prepareStatement(
							"update employee set Nama = ?,TempatLahir = ?,TanggalLahir = ?,JenisKelamin = ?, Jabatan = ?,MasaKerja = ?,Kontak = ?, Photo= ?, Alamat = ? where Nim = ? ");

			ps.setString(1, empl.getNama());
			ps.setString(2, empl.getTeTgl());
			ps.setString(3, empl.getTaTgl());
			ps.setString(4, empl.getjKelamin());
			ps.setString(5, empl.getJabatan());
			ps.setInt(6, empl.getmKerja());
			ps.setInt(7, empl.getKontak());
			ps.setString(8, empl.getPhoto());
			ps.setString(9, empl.getAlamat());
			ps.setInt(10, empl.getNim());

			result = ps.executeUpdate();

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			ps.close();
		}
		return result;
	}

	@Override
	public List<Empl> getUser() throws SQLException {
		List<Empl> empl = new LinkedList<Empl>();

		Statement st = null;
		ResultSet rs = null;
		Empl em = null;

		try {
			st = DatabaseUtilities.getInstance().getConnection()
					.createStatement();

			rs = st.executeQuery("select * from employee");
			while (rs.next()) {
				em = new Empl();

				em.setNim(Integer.valueOf(rs.getString("Nim")));
				em.setNama(rs.getString("Nama"));
				em.setTeTgl(rs.getString("TempatLahir"));
				em.setTaTgl(rs.getString("TanggalLahir"));
				em.setjKelamin(rs.getString("JenisKelamin"));
				em.setJabatan(rs.getString("Jabatan"));
				em.setmKerja(Integer.valueOf(rs.getString("MasaKerja")));
				em.setKontak(Integer.valueOf(rs.getString("Kontak")));
				em.setPhoto(rs.getString("Photo"));
				em.setAlamat(rs.getString("Alamat"));

				empl.add(em);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			st.close();
			rs.close();
		}
		return empl;
	}

	public List<Empl> getReport(String addWhere) throws SQLException {
		List<Empl> empl = new LinkedList<Empl>();

		Statement st = null;
		ResultSet rs = null;
		Empl em = null;

		try {
			st = DatabaseUtilities.getInstance().getConnection()
					.createStatement();

			rs = st.executeQuery("select * from employee where " + addWhere);
			while (rs.next()) {
				em = new Empl();

				em.setNim(Integer.valueOf(rs.getString("Nim")));
				em.setNama(rs.getString("Nama"));
				em.setTeTgl(rs.getString("TempatLahir"));
				em.setTaTgl(rs.getString("TanggalLahir"));
				em.setjKelamin(rs.getString("JenisKelamin"));
				em.setJabatan(rs.getString("Jabatan"));
				em.setmKerja(Integer.valueOf(rs.getString("MasaKerja")));
				em.setKontak(Integer.valueOf(rs.getString("Kontak")));
				em.setPhoto(rs.getString("Photo"));
				em.setAlamat(rs.getString("Alamat"));

				empl.add(em);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			st.close();
			rs.close();
		}
		return empl;
	}

	@Override
	public Empl getUser(String nim) throws SQLException {

		Statement st = null;
		ResultSet rs = null;
		Empl em = null;

		try {
			st = DatabaseUtilities.getInstance().getConnection()
					.createStatement();
			rs = st.executeQuery("select * from employee where nim = "
					+ Integer.valueOf(nim) + "");
			while (rs.next()) {
				em = new Empl();

				em.setNim(Integer.valueOf(rs.getString("Nim")));
				em.setNama(rs.getString("Nama"));
				em.setTeTgl(rs.getString("TempatLahir"));
				em.setTaTgl(rs.getString("TanggalLahir"));
				em.setjKelamin(rs.getString("JenisKelamin"));
				em.setJabatan(rs.getString("Jabatan"));
				em.setmKerja(Integer.valueOf(rs.getString("MasaKerja")));
				em.setKontak(Integer.valueOf(rs.getString("Kontak")));
				em.setPhoto(rs.getString("Photo"));
				em.setAlamat(rs.getString("Alamat"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			st.close();
			rs.close();
		}
		return em;
	}

	@Override
	public int delete(int nim) throws SQLException {
		int result = -1;
		PreparedStatement ps = null;
		try {
			ps = DatabaseUtilities.getInstance().getConnection()
					.prepareStatement("delete from employee where Nim = ?");

			ps.setInt(1, nim);
			result = ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			ps.close();
		}

		return result;
	}

	@Override
	public ResultSet cetakNim(String nim) throws SQLException {
		Statement st = null;
		ResultSet rs = null;

		try {
			st = DatabaseUtilities.getInstance().getConnection()
					.createStatement();
			rs = st.executeQuery("SELECT nama,nim,jabatan FROM employee where nim = "
					+ Integer.valueOf(nim) + "");

		} catch (Exception e) {
			e.printStackTrace();
		}
		return rs;
	}

	@Override
	public ResultSet cetakReport(String addWhere) throws SQLException {
		Statement st = null;
		ResultSet rs = null;

		try {
			st = DatabaseUtilities.getInstance().getConnection()
					.createStatement();
			rs = st.executeQuery("select * from employee where " + addWhere
					+ "");

		} catch (Exception e) {
			e.printStackTrace();
		}
		return rs;
	}
}
